//
//  LZMyHeadView.m
//  laziz_Consumer
//
//  Created by ZhaoDongBo on 2016/10/26.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZMyHeadView.h"
#import "LZMyHeadButton.h"

#import "Masonry.h"
#import "BlocksKit+UIKit.h"

#import "UIImageView+WebCache.h"
#import "LZUserModel.h"

@implementation LZMyHeadView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.backgroundColor = UIColorFromRGB(0xffffff, 1);
        [self setup];
        
        [self addAutoLayout];
    }
    return self;
}

- (void)setup {
    
    [self addSubview:self.bgImageView];
    
    [self addSubview:self.headButton];
    [self.headButton addSubview:self.userNameLabel];
    [self.headButton addSubview:self.headImageView];
    [self.headButton addSubview:self.markImageView];
    
    [self addSubview:self.myCouponsButton];
    [self addSubview:self.line];
    [self addSubview:self.myToBeReviewButton];
    [self addSubview:self.line2];
    [self addSubview:self.myReviewButton];
}

- (void)addAutoLayout {
    
    WS(weakSelf);
    
    [self.bgImageView makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(-480.0);
        make.leading.mas_equalTo(0);
        make.trailing.mas_equalTo(0);
        make.bottom.mas_equalTo(-154);
    }];
    
    [self.headButton makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(61);
        make.leading.mas_equalTo(50);
        make.trailing.mas_equalTo(-50);
        make.height.mas_equalTo(125);
    }];
    
    [self.line makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-9);
        make.leading.mas_equalTo(SCREEN_WIDTH/3.0);
        make.size.mas_equalTo(CGSizeMake(1, 76));
    }];
    
    [self.line2 makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-9);
        make.trailing.mas_equalTo(-SCREEN_WIDTH/3.0);
        make.size.mas_equalTo(CGSizeMake(1, 76));
    }];
    
    [self.myCouponsButton makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(0);
        make.leading.mas_equalTo(0);
        make.height.mas_equalTo(80);
        make.trailing.mas_equalTo(weakSelf.line.leading);
    }];
    
    [self.myToBeReviewButton makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(0);
        make.leading.mas_equalTo(weakSelf.line.trailing);
        make.height.mas_equalTo(80);
        make.width.mas_equalTo(weakSelf.myCouponsButton.mas_width);
    }];
    
    [self.myReviewButton makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(0);
        make.leading.mas_equalTo(weakSelf.line2.trailing);
        make.height.mas_equalTo(80);
        make.trailing.mas_equalTo(0);
    }];
    
    [self.userNameLabel makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(weakSelf.myCouponsButton.mas_top).with.offset(-22);
        make.centerX.mas_equalTo(-6);
        make.height.mas_equalTo(20);
        make.width.mas_lessThanOrEqualTo(SCREEN_WIDTH - 31);
    }];
    
    [self.headImageView makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(weakSelf.userNameLabel.mas_top).with.offset(-15);
        make.centerX.mas_equalTo(weakSelf.centerX);
        make.size.mas_equalTo(CGSizeMake(95, 95));
    }];
    
    [self.markImageView makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(weakSelf.userNameLabel.bottom).with.offset(-3);
        make.leading.mas_equalTo(weakSelf.userNameLabel.mas_trailing).with.offset(6);
        make.size.mas_equalTo(CGSizeMake(5, 5));
    }];
}

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    [self reloadSubview];
}

- (void)reloadSubview {
    if ([[LZUserModel Instance] isLogin]) {
        // 如果已登录
        LZCurrentUserInfo *userInfo = [LZUserModel Instance].userInfo;
        
        [self.myCouponsButton setNumberButtonNumTitle:userInfo.couponsNum];
        
        [self.myToBeReviewButton setNumberButtonNumTitle:userInfo.noReviewedNum];
        
        self.userNameLabel.text = userInfo.nickName;
        [self.headImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@", APIBaseImageUrl, userInfo.userPhoto]] placeholderImage:[UIImage imageNamed:@"lz_myuserdefaultimage"]];
    }
    else {
        // 如果未登录
        [self.myCouponsButton setNumberButtonNumTitle:0];
        
        [self.myToBeReviewButton setNumberButtonNumTitle:0];
        
        self.userNameLabel.text = LZLocalizedString(@"login_loginnow", nil);
        
        self.headImageView.image = [UIImage imageNamed:@"lz_myuserdefaultimage"];
    }
}

- (UIImageView *)bgImageView {

    if (_bgImageView == nil) {
        _bgImageView        = [[UIImageView alloc] init];
        _bgImageView.image  = [UIImage imageNamed:@"lz_mybgheadimage_bg"];
    }
    
    return _bgImageView;
}

- (UIButton *)headButton {

    if (_headButton == nil) {
        _headButton     = [[UIButton alloc] init];
        _headButton.tag = LZ_MY_HEAD_BUTTON_TAG_LOGIN;

        WS(weakSelf);
        [_headButton bk_addEventHandler:^(UIButton * sender) {
            
            if (weakSelf.myHeadViewButtonActionBlock) {
                weakSelf.myHeadViewButtonActionBlock(LZ_MY_HEAD_BUTTON_TAG_LOGIN);
            }
            
        } forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _headButton;
}

- (UIImageView *)headImageView {

    if (_headImageView == nil) {
        _headImageView          = [[UIImageView alloc] init];
        _headImageView.image    = [UIImage imageNamed:@"lz_myuserdefaultimage"];
        _headImageView.layer.masksToBounds  = YES;
        _headImageView.layer.cornerRadius   = 95/2.0;
        _headImageView.layer.borderWidth    = 2.5;
        _headImageView.layer.borderColor    = UIColorFromRGB(0xa9a6a3, 0.6).CGColor;
    }
    
    return _headImageView;
}

- (UILabel *)userNameLabel {

    if (_userNameLabel == nil) {
        _userNameLabel = [[UILabel alloc] init];
        _userNameLabel.text         = LZLocalizedString(@"login_loginnow", @"");//@"Login now";
        _userNameLabel.textColor    = UIColorFromRGB(0x333333, 1);
    }
    
    return _userNameLabel;
}

- (UIImageView *)markImageView {

    if (_markImageView == nil) {
        _markImageView          = [[UIImageView alloc] init];
        _markImageView.image    = [UIImage imageNamed:@"lz_myheadview_markimage"];
    }
    
    return _markImageView;
}

- (LZMyHeadButton *)myCouponsButton {

    if (_myCouponsButton == nil) {
        _myCouponsButton        = [[LZMyHeadButton alloc] initWithButtonType:LZ_MY_HEAD_BUTTON_TYPE_COUPONS];
        _myCouponsButton.tag    = LZ_MY_HEAD_BUTTON_TAG_COUPONS;

        WS(weakSelf);
        [_myCouponsButton bk_addEventHandler:^(UIButton * sender) {
            
            if (weakSelf.myHeadViewButtonActionBlock) {
                weakSelf.myHeadViewButtonActionBlock(LZ_MY_HEAD_BUTTON_TAG_COUPONS);
            }
            
        } forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _myCouponsButton;
}

- (UIView *)line {

    if (_line == nil) {
        _line = [[UIView alloc] init];
        _line.backgroundColor = UIColorFromRGB(0xeeeeee, 1);
    }
    
    return _line;
}

- (LZMyHeadButton *)myToBeReviewButton {

    if (_myToBeReviewButton == nil) {
        _myToBeReviewButton     = [[LZMyHeadButton alloc] initWithButtonType:LZ_MY_HEAD_BUTTON_TYPE_TOBEREVIEW];
        _myToBeReviewButton.tag = LZ_MY_HEAD_BUTTON_TAG_TOBEREVIEW;

        WS(weakSelf);
        [_myToBeReviewButton bk_addEventHandler:^(UIButton * sender) {
            
            if (weakSelf.myHeadViewButtonActionBlock) {
                weakSelf.myHeadViewButtonActionBlock(LZ_MY_HEAD_BUTTON_TAG_TOBEREVIEW);
            }
            
        } forControlEvents:UIControlEventTouchUpInside];
    }

    return _myToBeReviewButton;
}

- (UIView *)line2 {
    
    if (_line2 == nil) {
        _line2 = [[UIView alloc] init];
        _line2.backgroundColor = UIColorFromRGB(0xeeeeee, 1);
    }
    
    return _line2;
}

- (LZMyHeadButton *)myReviewButton {
    
    if (_myReviewButton == nil) {
        _myReviewButton     = [[LZMyHeadButton alloc] initWithButtonType:LZ_MY_HEAD_BUTTON_TYPE_REVIEW];
        _myReviewButton.tag = LZ_MY_HEAD_BUTTON_TAG_REVIEW;

        WS(weakSelf);
        [_myReviewButton bk_addEventHandler:^(UIButton * sender) {
            
            if (weakSelf.myHeadViewButtonActionBlock) {
                weakSelf.myHeadViewButtonActionBlock(LZ_MY_HEAD_BUTTON_TAG_REVIEW);
            }
    
        } forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _myReviewButton;
}

@end
